
************************************
*** FIGURE A6: Effect of EMR on Patient Outcomes by Clinic Type (Hospital vs. Clinic)
*** FIGURE A7: Effect of EMR on Patient Outcomes by Clinic Type (Large vs. Small)
*** FIGURE A8: Effect of EMR on Patient Outcomes by Clinic Type (Urban vs. Rural)
*** FIGURE A9 PANEL A
*** TABLE A14: Robustness: Heterogeneity by Clinic Type (Hospital vs. Clinic)
*** TABLE A15: Robustness: Heterogeneity by Clinic Size (Large vs. Small)
*** TABLE A16: Robustness: Heterogeneity by Clinic Location (Urban vs. Rural)
************************************


use Data/Analysis/Data_ClinicYears, clear


keep YearsPostEMR EMRyear Datey VisitSite logInCare* logDeath* logNew* logReturn* Post hospital urban large

gen Ei=EMRyear
gen t=Datey
gen K = t-Ei 								
gen D = K>=0 & Ei!=. 

gen i=VisitSite
tsset i t


// TWFE 

forvalues l = 0/6 {
	gen L`l'event = K==`l'
}
replace L6event=1 if K>6
forvalues l = 1/6 {
	gen F`l'event = K==-`l'
}
drop F1event


forvalues l = 0/6 {
	gen L`l'event_large=L`l'event*large
	gen L`l'event_nlarge=L`l'event*(1-large)
	gen L`l'event_urban=L`l'event*urban
	gen L`l'event_nurban=L`l'event*(1-urban)
	gen L`l'event_hospital=L`l'event*hospital
	gen L`l'event_nhospital=L`l'event*(1-hospital)
}
replace L6event=1 if K>6


local largeLabel "Large"
local nlargeLabel "Small"
local urbanLabel "Urban"
local nurbanLabel "Rural"
local hospitalLabel "Hospital"
local nhospitalLabel "Clinic"

cap erase Output/Tables/TableA14.xls
cap erase Output/Tables/TableA14.txt

foreach y of varlist  logDeath logInCare logNew logReturn {
	foreach x of varlist hospital {
		reghdfe `y'  L*event_`x' L*event_n`x', a(i t) cluster(i)
			estimates store e_`x'`y'
			outreg2 using Output/Tables/TableA14.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(L*event*) addtext(Year FE, Y, Clinic FE, Y) 

	}
}

local n = 1 
foreach y of varlist logInCare logReturn logDeath logNew {
	foreach x of varlist hospital {
		event_plot e_`x'`y' e_`x'`y', ///
			stub_lag(L#event_`x' L#event_n`x') stub_lead() plottype(scatter) ciplottype(rspike) ///
			together perturb(-0.1(0.2)0.1) trimlead(6) noautolegend ///
			graph_opt( ///
				xtitle("Year Index") ytitle("Coefficient Estimates") xlabel(-1 "-1" 0 "0" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "{&ge} 6") ylabel(-1(0.5)1) ///
			legend(pos(6) order(1 3  ///
			) label(1 ``x'Label') label(3 `n`x'Label') ///
			rows(1) region(style(none))) ///
			/// the following lines replace default_look with something more elaborate
			xline(-0.5, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
		) ///
		lag_opt1(msymbol(D) color(black)) lag_ci_opt1(color(black)) ///
		lag_opt2(msymbol(S) color(gray)) lag_ci_opt2(color(gray)) 
		
			graph save "Output/Figures/FigureA06_`n'.gph", replace
			graph export "Output/Figures/FigureA06_`n'.png", replace
		
		local n = `n' + 1 
	}
}


foreach y of varlist logDeath {
	foreach x of varlist hospital {
		event_plot e_`x'`y' e_`x'`y', ///
			stub_lag(L#event_`x' L#event_n`x') stub_lead() plottype(scatter) ciplottype(rspike) ///
			together perturb(-0.1(0.2)0.1) trimlead(6) noautolegend ///
			graph_opt( ///
				xtitle("Year Index") ytitle("Coefficient Estimates") xlabel(-1 "-1" 0 "0" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "{&ge} 6") ylabel(-1(0.5)1) ///
			legend(pos(6) order(1 3  ///
			) label(1 ``x'Label') label(3 `n`x'Label') ///
			rows(1) region(style(none))) ///
			/// the following lines replace default_look with something more elaborate
			xline(-0.5, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
		) ///
		lag_opt1(msymbol(D) color(black)) lag_ci_opt1(color(black)) ///
		lag_opt2(msymbol(S) color(gray)) lag_ci_opt2(color(gray)) 
		graph save "Output/Figures/FigureA09PanelA.gph", replace
		graph export "Output/Figures/FigureA09PanelA.png", replace
	}
}



cap erase Output/Tables/TableA15.xls
cap erase Output/Tables/TableA15.txt

foreach y of varlist  logDeath logInCare logNew logReturn {
	foreach x of varlist large {
		reghdfe `y'  L*event_`x' L*event_n`x', a(i t) cluster(i)
			estimates store e_`x'`y'
			outreg2 using Output/Tables/TableA15.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(L*event*) addtext(Year FE, Y, Clinic FE, Y) 

	}
}

local n = 1
foreach y of varlist logInCare logReturn logDeath logNew {
	foreach x of varlist large {
		event_plot e_`x'`y' e_`x'`y', ///
			stub_lag(L#event_`x' L#event_n`x') stub_lead() plottype(scatter) ciplottype(rspike) ///
			together perturb(-0.1(0.2)0.1) trimlead(6) noautolegend ///
			graph_opt( ///
				xtitle("Year Index") ytitle("Coefficient Estimates") xlabel(-1 "-1" 0 "0" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "{&ge} 6") ylabel(-1(0.5)1) ///
			legend(pos(6) order(1 3  ///
			) label(1 ``x'Label') label(3 `n`x'Label') ///
			rows(1) region(style(none))) ///
			/// the following lines replace default_look with something more elaborate
			xline(-0.5, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
		) ///
		lag_opt1(msymbol(D) color(black)) lag_ci_opt1(color(black)) ///
		lag_opt2(msymbol(S) color(gray)) lag_ci_opt2(color(gray)) 
		
			graph save "Output/Figures/FigureA07_`n'.gph", replace
			graph export "Output/Figures/FigureA07_`n'.png", replace
	
		local n = `n' + 1 
	}
}




cap erase Output/Tables/TableA16.xls
cap erase Output/Tables/TableA16.txt

foreach y of varlist  logDeath logInCare logNew logReturn {
	foreach x of varlist urban {
		reghdfe `y'  L*event_`x' L*event_n`x', a(i t) cluster(i)
			estimates store e_`x'`y'
			outreg2 using Output/Tables/TableA16.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(L*event*) addtext(Year FE, Y, Clinic FE, Y) 

	}
}



local n = 1 
foreach y of varlist logInCare logReturn logDeath logNew {
	foreach x of varlist urban {
		event_plot e_`x'`y' e_`x'`y', ///
			stub_lag(L#event_`x' L#event_n`x') stub_lead() plottype(scatter) ciplottype(rspike) ///
			together perturb(-0.1(0.2)0.1) trimlead(6) noautolegend ///
			graph_opt( ///
				xtitle("Year Index") ytitle("Coefficient Estimates") xlabel(-1 "-1" 0 "0" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "{&ge} 6") ylabel(-1(0.5)1) ///
			legend(pos(6) order(1 3  ///
			) label(1 ``x'Label') label(3 `n`x'Label') ///
			rows(1) region(style(none))) ///
			/// the following lines replace default_look with something more elaborate
			xline(-0.5, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
		) ///
		lag_opt1(msymbol(D) color(black)) lag_ci_opt1(color(black)) ///
		lag_opt2(msymbol(S) color(gray)) lag_ci_opt2(color(gray)) 
		
			graph save "Output/Figures/FigureA08_`n'.gph", replace
			graph export "Output/Figures/FigureA08_`n'.png", replace
		
		local n = `n' + 1  
	}
}



